#include"s3c2440.h"
#include"uart.h"
#include"timer.h"


/*
*TimerId:0~4
*/
void TimerInit(unsigned int TimerId,
               unsigned int Prescaler, unsigned int Divider,
               unsigned int CountBuf, unsigned int CmpBuf)
{
    if (TimerId <= 1)
    {
        TCFG0 &= ~PRESCAL_MSK;
        TCFG0 |= Prescaler;
    }
    else
    {
        TCFG0 &= ~(PRESCAL_MSK << 8);
        TCFG0 |= (Prescaler << 8);
    }
    TCFG1 &= ~(0XF << 20);
    switch (TimerId)
    {
    case 0:
        {
            TCFG1 &= ~MUX_MSK;
            TCFG1 |= Divider;
            TCNTB0 = CountBuf;
            TCMPB0 = CmpBuf;
            break;
        }
    case 1:
        {
            TCFG1 &= ~(MUX_MSK << 4);
            TCFG1 |= (Divider << 4);
            TCNTB1 = CountBuf;
            TCMPB1 = CmpBuf;
            break;
        }
    case 2:
        {
            TCFG1 &= ~(MUX_MSK << 8);
            TCFG1 |= (Divider << 8);
            TCNTB2 = CountBuf;
            TCMPB2 = CmpBuf;
            break;
        }
    case 3:
        {
            TCFG1 &= ~(MUX_MSK << 12);
            TCFG1 |= (Divider << 12);
            TCNTB3 = CountBuf;
            TCMPB3 = CmpBuf;
            break;
        }
    case 4:
        {
            TCFG0 &= ~(MUX_MSK << 16);
            TCFG0 |= (Divider << 16);
            TCNTB4 = CountBuf;  //timer4 has no pwm
            break;
        }
    }
}

void TimerManUp(unsigned int TimerId, unsigned char Ture)
{
    if (1 == Ture)
    {
        if (0 == TimerId)
        {
            TCON |= (1 << 1);
        }
        else
        {
                       TCON |= (1 << ((TimerId-1)*4 + 9));
        }
    }
    else
    {
        if (0 == TimerId)
        {
            TCON &= ~(1 << 1);
        }
        else
        {
            TCON &= ~(1 << ((TimerId-1)*4 + 9));
        }
    }
}

void TimerStart(unsigned int TimerId,unsigned char Ture)
{
    if (Ture)
    {
        if (0 == TimerId)
        {
            TCON |= (1 << 0);
        }
        else
        {
            TCON |= (1 << ((TimerId-1)*4 + 8));
        }
    }
    else if (0 == Ture)
    {
        if (0 == TimerId)
        {
            TCON &= ~(1 << 0);
        }
        else
        {
            TCON &= ~(1 << ((TimerId-1)*4 + 8));
        }
    }
}

void TimerAutoLoad(unsigned int TimerId,unsigned char Ture)
{
    if (1 == Ture)
    {
        if (0 == TimerId)
        {
            TCON |= (1 << 3);
        }
        else if (4 == TimerId)
        {
            TCON |= (1 << 22);
        }
        else
        {
            TCON |= (1 << ((TimerId-1)*4 + 11));
        }
    }
    else
    {
        if (0 == TimerId)
        {
            TCON &= ~(1 << 3);
        }
        else if (4 == TimerId)
        {
            TCON &= ~(1 << 22);

        }
        else
        {
            TCON &= ~(1 << ((TimerId-1)*4 + 11));
        }
    }
}
unsigned char TimerWaitFinish(unsigned int TimerId)
{
    unsigned int TcntAddr;
    switch (TimerId)
    {
    case 0:
        {
            TcntAddr = 0x51000014;
            break;
        }
    case 1:
        {
            TcntAddr = 0x51000020;
            break;
        }
    case 2:
        {
            TcntAddr = 0x5100002c;
            break;
        }
    case 3:
        {
            TcntAddr = 0x51000038;
            break;
        }
    case 4:
        {
            TcntAddr = 0x51000040;
            break;
        }
    }
    while((*(volatile unsigned int*)TcntAddr) != 0)
    {

    }
    return 1;
}
void TimerTest(void)
{
    unsigned int timerid;
    unsigned int prescal;
    unsigned int divider;
    unsigned int countbuf;
    unsigned int cmpbuf;
    unsigned int i;
    timerid = 0;
    prescal = 200;
    divider = 3;
    countbuf = 6000;
    cmpbuf = 10;
    puts("test timer0\n");
    for (timerid = 0; timerid < 5; timerid++)
    {
        puts("begin test timer");
        send_hex(timerid);putch('\n');
        TimerInit(timerid,prescal,divider,countbuf,cmpbuf);
        TimerManUp(timerid,1);
        TimerStart(timerid,1);
        TimerManUp(timerid,0);
        TimerAutoLoad(timerid,1);
        i = 0;
        while(1)
        {
            if(1 == TimerWaitFinish(timerid))
            {
                i++;
            }
            if(i == 50)
            {
                break;
            }
        }

        TimerStart(timerid,0);
        //puts("test Timer Over\n");

    }
}
